package com.imut.lagain.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imut.lagain.entity.FinalPassword;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 鏈€缁堝瘑鐮佹暟鎹闂眰
 */
@Mapper
public interface FinalPasswordRepository extends BaseMapper<FinalPassword> {
    
    /**
     * 鏌ヨ褰撳墠婵€娲荤殑瀵嗙爜
     * @return 婵€娲荤殑瀵嗙爜
     */
    @Select("SELECT * FROM final_password WHERE is_active = 1 AND is_deleted = 0 ORDER BY create_time DESC LIMIT 1")
    FinalPassword findActivePassword();
    
    /**
     * 鏌ヨ鎵€鏈夋縺娲荤殑瀵嗙爜鍒楄〃
     * @return 婵€娲荤殑瀵嗙爜鍒楄〃
     */
    @Select("SELECT * FROM final_password WHERE is_active = 1 AND is_deleted = 0 ORDER BY create_time DESC")
    List<FinalPassword> findAllActivePasswords();
    
    /**
     * 鏍规嵁瀵嗙爜鍊兼煡璇紙鐢ㄤ簬楠岃瘉锛?
     * @param password 瀵嗙爜鍊?
     * @return 瀵嗙爜淇℃伅
     */
    @Select("SELECT * FROM final_password WHERE password = #{password} AND is_deleted = 0")
    FinalPassword findByPassword(@Param("password") String password);
    
    /**
     * 鏍规嵁婵€娲荤姸鎬佹煡璇㈠瘑鐮佸垪琛?
     * @param isActive 鏄惁婵€娲?
     * @param page 鍒嗛〉鍙傛暟
     * @return 瀵嗙爜鍒嗛〉鍒楄〃
     */
    @Select("SELECT * FROM final_password WHERE is_active = #{isActive} AND is_deleted = 0 ORDER BY create_time DESC")
    IPage<FinalPassword> findByActiveStatus(@Param("isActive") Boolean isActive, Page<FinalPassword> page);
    
    /**
     * 鏌ヨ鎸囧畾鏃堕棿鑼冨洿鍐呭垱寤虹殑瀵嗙爜
     * @param startTime 寮€濮嬫椂闂?
     * @param endTime 缁撴潫鏃堕棿
     * @return 瀵嗙爜鍒楄〃
     */
    @Select("SELECT * FROM final_password WHERE create_time BETWEEN #{startTime} AND #{endTime} AND is_deleted = 0 ORDER BY create_time DESC")
    List<FinalPassword> findByCreateTimeBetween(@Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);
    
    /**
     * 鏌ヨ鎸囧畾鏃堕棿鑼冨洿鍐呭垱寤虹殑瀵嗙爜锛堝垎椤碉級
     * @param startTime 寮€濮嬫椂闂?
     * @param endTime 缁撴潫鏃堕棿
     * @param page 鍒嗛〉鍙傛暟
     * @return 瀵嗙爜鍒嗛〉鍒楄〃
     */
    @Select("SELECT * FROM final_password WHERE create_time BETWEEN #{startTime} AND #{endTime} AND is_deleted = 0 ORDER BY create_time DESC")
    IPage<FinalPassword> findByCreateTimeBetweenWithPage(@Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime, Page<FinalPassword> page);
    
    /**
     * 缁熻婵€娲荤殑瀵嗙爜鏁伴噺
     * @return 婵€娲诲瘑鐮佹暟閲?
     */
    @Select("SELECT COUNT(*) FROM final_password WHERE is_active = 1 AND is_deleted = 0")
    Long countActivePasswords();
    
    /**
     * 缁熻鎬诲瘑鐮佹暟閲?
     * @return 鎬诲瘑鐮佹暟閲?
     */
    @Select("SELECT COUNT(*) FROM final_password WHERE is_deleted = 0")
    Long countTotalPasswords();
    
    /**
     * 鏇存柊瀵嗙爜婵€娲荤姸鎬?
     * @param passwordId 瀵嗙爜ID
     * @param isActive 鏄惁婵€娲?
     * @return 褰卞搷琛屾暟
     */
    @Update("UPDATE final_password SET is_active = #{isActive}, update_time = NOW() WHERE id = #{passwordId}")
    int updateActiveStatus(@Param("passwordId") Long passwordId, @Param("isActive") Boolean isActive);
    
    /**
     * 鍋滅敤鎵€鏈夊瘑鐮?
     * @return 褰卞搷琛屾暟
     */
    @Update("UPDATE final_password SET is_active = 0, update_time = NOW() WHERE is_active = 1 AND is_deleted = 0")
    int deactivateAllPasswords();
    
    /**
     * 鏍规嵁鎻忚堪妯＄硦鏌ヨ瀵嗙爜
     * @param description 鎻忚堪鍏抽敭瀛?
     * @param page 鍒嗛〉鍙傛暟
     * @return 瀵嗙爜鍒嗛〉鍒楄〃
     */
    @Select("SELECT * FROM final_password WHERE description LIKE CONCAT('%', #{description}, '%') AND is_deleted = 0 ORDER BY create_time DESC")
    IPage<FinalPassword> findByDescriptionLike(@Param("description") String description, Page<FinalPassword> page);
    
    /**
     * 妫€鏌ュ瘑鐮佹槸鍚﹀瓨鍦?
     * @param password 瀵嗙爜鍊?
     * @return 鏄惁瀛樺湪
     */
    @Select("SELECT COUNT(*) > 0 FROM final_password WHERE password = #{password} AND is_deleted = 0")
    boolean existsByPassword(@Param("password") String password);
    
    /**
     * 妫€鏌ユ縺娲荤殑瀵嗙爜鏄惁瀛樺湪
     * @param password 瀵嗙爜鍊?
     * @return 鏄惁瀛樺湪涓旀縺娲?
     */
    @Select("SELECT COUNT(*) > 0 FROM final_password WHERE password = #{password} AND is_active = 1 AND is_deleted = 0")
    boolean existsByPasswordAndActive(@Param("password") String password);
    
    /**
     * 鑾峰彇鏈€鏂板垱寤虹殑瀵嗙爜
     * @return 鏈€鏂板瘑鐮?
     */
    @Select("SELECT * FROM final_password WHERE is_deleted = 0 ORDER BY create_time DESC LIMIT 1")
    FinalPassword findLatestPassword();
    
    /**
     * 鑾峰彇鏈€鏂版洿鏂扮殑瀵嗙爜
     * @return 鏈€鏂版洿鏂扮殑瀵嗙爜
     */
    @Select("SELECT * FROM final_password WHERE is_deleted = 0 ORDER BY update_time DESC LIMIT 1")
    FinalPassword findLatestUpdatedPassword();
    
    /**
     * 妫€鏌ュ瘑鐮佸€兼槸鍚﹀瓨鍦?
     * @param passwordValue 瀵嗙爜鍊?
     * @return 鏄惁瀛樺湪
     */
    @Select("SELECT COUNT(*) > 0 FROM final_password WHERE password = #{passwordValue} AND is_deleted = 0")
    boolean existsByPasswordValue(@Param("passwordValue") String passwordValue);
    
    /**
     * 鏌ヨ鏈€鏂板垱寤虹殑瀵嗙爜
     * @return 鏈€鏂板垱寤虹殑瀵嗙爜
     */
    @Select("SELECT * FROM final_password WHERE is_deleted = 0 ORDER BY create_time DESC LIMIT 1")
    FinalPassword findLatestCreatedPassword();
    
    /**
     * 鏌ヨ褰撳墠婵€娲荤殑瀵嗙爜
     * @return 褰撳墠婵€娲荤殑瀵嗙爜
     */
    @Select("SELECT * FROM final_password WHERE is_active = 1 AND is_deleted = 0 ORDER BY create_time DESC LIMIT 1")
    FinalPassword findCurrentActivePassword();
    
    /**
     * 鏍规嵁瀵嗙爜鍊兼煡璇㈠瘑鐮?
     * @param passwordValue 瀵嗙爜鍊?
     * @return 瀵嗙爜瀵硅薄
     */
    @Select("SELECT * FROM final_password WHERE password = #{passwordValue} AND is_deleted = 0")
    FinalPassword findByPasswordValue(@Param("passwordValue") String passwordValue);
}
